import { TIME_96, MARKET_COLOR_MAP } from '@/pages/ETrade/components/helper';
import { getCssVariableValue } from '@/utils/utils';

export const tableHead = [
  { code: 'point', name: '时点', width: 55, align: 'center' },
  { code: 'quantity', name: '出清电量 (MWh)', width: 100, align: 'center' },
  { code: 'price', name: '出清电价 (元/MWh)', width: 100, align: 'center' },
];

/**
 * 数据处理
 * @param {*} _data 原始数据
 * @param {number} _type // 0全部 1日前 2实时
 * @returns {*} { 图表配置, 表格数据 }
 */
export const fmtData = (_data, _type) => {
  if (!_data)
    return {
      chartOption: null,
      tableData: [],
    };
  const fmtNum = (v, fix) => {
    if (v === 0) return 0;
    if (!v) return '-';
    return v.toFixed(fix);
  };
  const marketNames = Object.keys(_data);

  let series = []; // 图表系列
  let sumList = Array.from({ length: 96 }, () => ({})); // 汇总
  marketNames.map((name) => {
    // name: 电网名称
    const color = MARKET_COLOR_MAP?.[name];
    const list = _data[name] || [];
    const quantityList = list.map((i, n) => {
      sumList[n].quantity = (sumList[n]?.quantity || 0) + i.clearingQuantity;
      sumList[n].cost = (sumList[n]?.cost || 0) + i.clearingIncome;
      return i.clearingQuantity;
    });
    // const quantityList = list.map((x, i) => {
    //   let quantity = 0,
    //     cost = 0; // 电量、电费
    // if (_type === 0) {
    //   quantity =
    //     x.transprovincialDayaheadClearingQuantity + x.transprovincialIntradayClearingQuantity;
    //   cost =
    //     x.transprovincialDayaheadClearingQuantity * x.transprovincialDayaheadClearingPrice +
    //     x.transprovincialIntradayClearingQuantity * x.transprovincialIntradayClearingPrice;
    // } else if (_type === 1) {
    //   quantity = x.transprovincialDayaheadClearingQuantity;
    //   cost = x.transprovincialDayaheadClearingQuantity * x.transprovincialDayaheadClearingPrice;
    // } else if (_type === 2) {
    //   quantity = x.transprovincialIntradayClearingQuantity;
    //   cost = x.transprovincialIntradayClearingQuantity * x.transprovincialIntradayClearingPrice;
    // }
    // cost = Number(cost) || 0;
    // quantity = Number(quantity) || 0;
    // sumList[i].cost = (sumList[i]?.cost || 0) + cost;
    // sumList[i].quantity = (sumList[i]?.quantity || 0) + quantity;
    // return fmtNum(quantity, 3);
    // });

    series.push({
      name,
      color,
      type: 'bar',
      data: quantityList,
      props: {
        stack: '出清电量',
        barCategoryGap: '50%',
      },
    });
  });
  // 出清电价=电费/电量
  const priceList = sumList.map((x) => fmtNum(x.cost / x.quantity, 2));
  // const priceList = sumList.map((x) => fmtNum(x.price, 2));
  series.push({
    type: 'line',
    name: '出清价格',
    color: '#F5BE15',
    data: priceList,
    props: {
      yAxisIndex: 1,
    },
  });
  const tableData = priceList.map((price, i) => ({
    price,
    quantity: fmtNum(sumList[i].quantity, 3),
    point: TIME_96[i],
  }));
  return {
    tableData,
    chartOption: {
      series,
      grid: { top: 24, left: 48, bottom: 48, right: 48 },
      tooltip: {
        enterable: true,
        extraCssText: 'max-height: 75%; overflow: hidden scroll;',
      },
      legend: {
        textStyle: {
          color: getCssVariableValue('--chart-legend-color'),
        },
        pageIconColor: getCssVariableValue('--bd-primary'),
        pageTextStyle: {
          color: getCssVariableValue('--chart-legend-color'),
        },
      },
      xAxis: [
        {
          type: 'category',
          data: TIME_96,
          axisLine: {
            lineStyle: {
              color: getCssVariableValue('--chart-x-line'),
            },
          },
          axisTick: {
            lineStyle: {
              color: getCssVariableValue('--chart-x-line'),
            },
          },
          axisLabel: {
            color: getCssVariableValue('--chart-font-color'),
          },
          axisPointer: {
            type: 'line',
            lineStyle: {
              color: getCssVariableValue('--chart-x-line'),
            },
          },
        },
      ],
      yAxis: [
        {
          type: 'value',
          name: 'MWh',
          nameTextStyle: {
            color: getCssVariableValue('--chart-font-color'),
          },
          splitLine: {
            lineStyle: {
              color: getCssVariableValue('--chart-y-line'),
            },
          },
          axisLabel: {
            color: getCssVariableValue('--chart-font-color'),
          },
        },
        {
          type: 'value',
          name: '元/MWh',
          nameTextStyle: {
            color: getCssVariableValue('--chart-font-color'),
          },
          splitLine: {
            lineStyle: {
              color: getCssVariableValue('--chart-y-line'),
            },
          },
          axisLabel: {
            color: getCssVariableValue('--chart-font-color'),
          },
        },
      ],
    },
  };
};
